package com.bw.miaosha.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bw.common.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.Query;

import com.bw.miaosha.dao.MiaoshaActivityDao;
import com.bw.miaosha.entity.MiaoshaActivityEntity;
import com.bw.miaosha.service.MiaoshaActivityService;


@Service("miaoshaActivityService")
public class MiaoshaActivityServiceImpl extends ServiceImpl<MiaoshaActivityDao, MiaoshaActivityEntity> implements MiaoshaActivityService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedissonClient redissonClient;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String page1 = (String) params.get("page");
        QueryWrapper<MiaoshaActivityEntity> wrapper = new QueryWrapper<>();
        //redis 查询有没有值
        IPage<MiaoshaActivityEntity> goodList = (IPage<MiaoshaActivityEntity>) redisTemplate.boundHashOps("goodList").get("list");

        if (goodList==null && Integer.parseInt(page1)==1){

            IPage<MiaoshaActivityEntity> page = this.page(
                    new Query<MiaoshaActivityEntity>().getPage(params),
                    wrapper
            );
            //存入redis
            redisTemplate.boundHashOps("goodList").put("list",page);
            System.out.println("数据库查");
            return new PageUtils(page);
        }else{
            System.out.println("缓存命中");
            return new PageUtils(goodList);
        }


    }

    @Override
    public R slist(Map<String, Object> params) {
        IPage<MiaoshaActivityEntity> page=new Page<>();
        String pagee = (String) params.get("page");
        String size = (String) params.get("limit");
        System.out.println(pagee);
        if (!StringUtils.isEmpty(pagee)){
            String o = stringRedisTemplate.opsForValue().get("goodsList" + pagee);
            System.out.println(o);
            if (StringUtils.isNotEmpty(o)){
                List<MiaoshaActivityEntity> pagess = JSONObject.parseObject(o, new TypeReference<List<MiaoshaActivityEntity>>() {
                });
                System.out.println("查redis");
                page.setRecords(pagess);
            }else{
                //在redis中没有缓存再去数据库进行查询
                System.out.println("查询一次数据库");
                QueryWrapper<MiaoshaActivityEntity> wrapper = new QueryWrapper<>();
                IPage<MiaoshaActivityEntity> pageee = this.page(
                        new Query<MiaoshaActivityEntity>().getPage(params),
                        wrapper
                );
                List<MiaoshaActivityEntity> records = pageee.getRecords();
                String jsonString = JSONObject.toJSONString(records);
                //设置过期时间为30分钟
                stringRedisTemplate.opsForValue().set("goodsList" + pagee,jsonString,20, TimeUnit.SECONDS);
                page=pageee;
            }

        }
        return R.ok().put("page",new PageUtils(page));
    }

}